import React from 'react'
import Editor from 'for-editor'

interface Props {
    value: string;
    onChange: (editorState: string) => void;
}
interface State{
    editorValue: string;
    imageUrl: string;
}
class WriteBlog extends React.Component<Props, State> {
    constructor(props: Props) {
        super(props)
        this.state = {
            editorValue: '',
            imageUrl: ''
        }
    }
    componentDidMount() {
        this.setState({
            editorValue: this.props.value
        })
    }
    handleChange(value: string) {
        this.setState({
            editorValue: value
        })
        this.props.onChange(value)
    }
    uploadHandler(params: any) {
        // fetch.get('getQiniuToken', {
        //     token: JSON.parse(Cookies.get('loginInfo')).token
        // }).then(res => {
        //     utils.uploadFile(params, res.data.qiniuToken).then(res => {
        //         this.setState({
        //             imageUrl: 'http://img.xuweijin.com/' + res
        //         })
        //         let str = this.state.editorValue + '![alt](http://img.xuweijin.com/' + res + ')'
        //         this.setState({
        //             editorValue: str
        //         })
        //     })
        // })
    }
    render() {
        return (
            <Editor
                subfield={true}
                preview={true}
                addImg={(file: File) => this.uploadHandler(file)}
                value={this.state.editorValue} onChange={(value: string) => this.handleChange(value)} />
        )
    }
}

export default WriteBlog